/**
 * ...
 * @author Thot
 */

function LibraryMediator( p_view )
{
	this.init( LibraryMediator.NAME, p_view );
}

POO.extend( LibraryMediator, Mediator );


	// static
	LibraryMediator.NAME = "LibraryMediator";
	
	
	//private
	LibraryMediator.prototype._screen = null;
	
	// public
	LibraryMediator.prototype.init = function( p_name, p_data )
	{
		this._super.init.apply( this, [p_name, p_data] );
		this._screen = new LibraryScreen();
		this.listenNotifications();	
		
		var container = this.getViewComponent();
		var list = document.getElementById("animationList");
		var preview = document.getElementById("preview");
		this._screen.init( list, preview );
		this._screen.signal.addListener( LibraryScreen.ON_SELECT, this.selectAnimationHandler, this );
	};
	
	LibraryMediator.prototype.listenNotifications = function()
	{
		this.addNotificationListener( ApplicationNotification.LOAD_DATA_COMPLETE, 	this.onNotification );
		this.addNotificationListener( ApplicationNotification.UNLOAD_PROJECT, 		this.onNotification );
	};

	LibraryMediator.prototype.onNotification = function( p_notificationType, p_data )
	{
		switch ( p_notificationType )
		{
			case ApplicationNotification.LOAD_DATA_COMPLETE:
				this.listAnimations();
				break;
				
			case ApplicationNotification.UNLOAD_PROJECT:
				this.clean();
				break;
		}
	};
	
	LibraryMediator.prototype.selectAnimationHandler = function(p_animId)
	{
		var animProxy = this.getFacade().getProxy( AnimationProxy.NAME );
		var animVO = animProxy.getAnimationVOById(p_animId);
		
		if( animVO == null )
			return;
				
		this._screen.renderAnimation( animVO );
		this.getFacade().sendNotification( ApplicationNotification.SELECT_ANIMATION, p_animId );
	};
	
	LibraryMediator.prototype.clean = function()
	{
		this._screen.clean();
	};
	
	LibraryMediator.prototype.listAnimations = function()
	{
		var animProxy = this.getFacade().getProxy( AnimationProxy.NAME );
		var animVO = null;
		var data = animProxy.getData();
		var prop = null;
		var i = 0;
		var max = data.length;
		
		this.clean();
		
		for( ; i < max; i++ )
		{
			vo = data[i];
			this._screen.addAnimation( vo.name );
		}
	}
